'''
Written by Jesse Merritt
October 1, 2011

This program will send a serial conmmand to a board to control 3 power outlets.

The GUI requires WX.

The program depends on the following modules:
serial:	Controls data over the virtual serial port
wx:  	Manages the GUI
wx.lib.newevent:  Event manager

Change Log:
------------------------------------------------------------------------------------------------------


'''
import serial 
import wx
import sys
import os


version = "0.1"
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1)
#serialEVT, EVT_SERIAL = wx.lib.newevent.NewEvent()          # this is the notification event to let us know the data has been updated
'''
Main window.  Creates all of the frames and binds buttons to functions.  Manages GUI
'''
class MainWindow(wx.Frame):
    def __init__(self, parent, title="Simple Electrical Outlet Control") :    
        self.parent = parent       
        mainFrame = wx.Frame.__init__(self,self.parent, title=title, size=(640,480))         
        
        mainPanel = wx.Panel(self, -1, style=wx.SUNKEN_BORDER)
        
        #   Build sizers and statusbar
        self.rootSizer = wx.BoxSizer(wx.HORIZONTAL)     
                        
        self.CreateStatusBar()                              # statusbar in the bottom of the window                                  

        # Setting up the menus
        filemenu= wx.Menu()
        setupmenu = wx.Menu()
        helpmenu = wx.Menu()       

        menuSave = filemenu.Append(wx.ID_SAVE, "Save", "Save the current data")     
        filemenu.AppendSeparator()
        menuExit = filemenu.Append(wx.ID_EXIT,"E&xit"," Terminate the program")     
        menuAbout = helpmenu.Append(wx.ID_ABOUT, "&About"," Information about this program")  

        # Menubar
        menuBar = wx.MenuBar()
        menuBar.Append(filemenu,"&File")                    # Adding the "filemenu" to the MenuBar       
        menuBar.Append(helpmenu, "Help")
        self.SetMenuBar(menuBar)                            # Adding the MenuBar to the Frame content.
 
        buttonOne = wx.Button(mainPanel, -1, 'Outlet 1',size=(100,100))
        buttonTwo = wx.Button(mainPanel, -1, 'Outlet 2',size=(100,100))
        buttonThree = wx.Button(mainPanel, -1, 'Outlet 3',size=(100,100))

        #  Sizers.  Everything is on rootSizer                     
        self.rootSizer.Add(buttonOne, 1, wx.EXPAND)
        self.rootSizer.Add(buttonTwo, 1, wx.EXPAND)
        self.rootSizer.Add(buttonThree, 1, wx.EXPAND)
        

        #   Bind events and buttons
#        self.Bind(EVT_SERIAL, self.updateText)                  # bind the event to a function
        self.Bind(wx.EVT_CLOSE, self.OnExit)
#        self.Bind(wx.EVT_MENU, self.OnAbout, menuAbout)
        self.Bind(wx.EVT_MENU, self.OnExit, menuExit)
        #self.Bind(wx.EVT_MENU, self.setupPort, menuPorts)
#        self.Bind(wx.EVT_MENU, self.OnSave, menuSave)
        self.Bind(wx.EVT_BUTTON, self.one,buttonOne)
        self.Bind(wx.EVT_BUTTON, self.two,buttonTwo)
        self.Bind(wx.EVT_BUTTON, self.three,buttonThree)

        # set the sizers
        self.SetSizer(self.rootSizer)
        self.SetAutoLayout(1)
        self.rootSizer.Fit(self)
             
        
        self.Layout()
	self.Show(True)
        
    def showError(self) :     # Didn't find the board.  
        dlg = wx.MessageDialog(self, "Bad Data.  The data received was not what was sent!", 'Error!', wx.OK | wx.ICON_ERROR)  
        dlg.ShowModal()
      #  self.OnExit(self)
 
    def OnExit(self,e):         # stuff to do when the program is ending     
        global ser     
        ser.close()  
        self.Destroy()              # wipe out windows and dump to the OS

    def one (self, e) :   # # button one
      ser.write("1")
      inByte = ser.read()
      print inByte
      if inByte != "1" :
	self.showError()	
     
    def two (self, e) :
      ser.write("2")
      inByte = ser.read()
      print inByte
      if inByte != "2" :
	self.showError()
      
    def three (self, e) :
      ser.write("3")
      inByte = ser.read()
      print inByte
      if inByte != "3" :
	self.showError()
    

app = wx.App(False)         # wx instance
frame = MainWindow(None)    # main window frame

app.MainLoop()

